# 第一阶段：安装依赖
FROM python:3.9-slim AS build

# 替换为阿里云的APT源
RUN sed -i 's|http://deb.debian.org/debian|http://mirrors.aliyun.com/debian|g' /etc/apt/sources.list.d/debian.sources && \
  sed -i 's|http://security.debian.org/debian-security|http://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list.d/debian.sources

WORKDIR /install

COPY requirements.txt /install/

# 更新 pip 并安装 Python 依赖
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir --upgrade pip \
  && pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir --upgrade --prefer-binary -r requirements.txt

# 安装额外的包
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir --upgrade --prefer-binary easyquotation tushare mootdx pytdx pywencai

# 第二阶段：构建最终镜像
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9

# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 设置 PYTHONPATH 环境变量
ENV PYTHONPATH="/app"

WORKDIR /app

COPY . /app

COPY --from=build /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages

RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir --upgrade pip
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir --upgrade easyquotation tushare mootdx

# 容器启动时运行 FastAPI 服务器
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
